package com.mobilebook.backend.action;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.mobilebook.backend.dao.AdminUserDao;
import com.mobilebook.backend.objects.AdminUser;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class DefaultAction extends ActionSupport{
	protected AdminUserDao adminUserDao;

	public AdminUserDao getAdminUserDao() {
		return adminUserDao;
	}

	public void setAdminUserDao(AdminUserDao adminUserDao) {
		this.adminUserDao = adminUserDao;
	}

	public String index(){
		ActionContext ac = ActionContext.getContext();
		Map session = ac.getSession();
		AdminUser user = (AdminUser) session.get("adminuser");
		if(user != null)
			return null;
		HttpServletResponse response = null;  
		/*try {  
            response = ServletActionContext.getResponse();  
            HttpServletRequest request = ServletActionContext.getRequest();
            Cookie[] cookies = request.getCookies();
            for (int i = 0; i < cookies.length; i++)     
            {    
                 Cookie c = cookies[i];      
                 if(c.getName()!= null &&c.getName().equalsIgnoreCase("username")){
                	 
                	 if(c.getValue() != null && c.getValue().length()>0){
                		 user = adminUserDao.getAdminByAccount(c.getValue());
                		 if(user != null)
                		 {
                			 session.put("adminuser", user);
                			 return null;
                		 }
                	 }
                	 break;
                 }
            }
		}catch(Exception exp){
			
		}*/
		return "login";
	}
}
